﻿package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.serializable.PostActivityData;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class PostActivity3Activity extends BaseActivity {

	private int[] backgroundColor;
	private Results results;
	private PostActivityData postActivityData;
	private EditText post_activity3_name, post_activity3_contents,
			post_activity3_jinbinum, post_activity3_personnum;
	private Spinner post_activity3_quanxian_set;
	private CheckBox post_activity3_is_dongtai;
	private LinearLayout post_activity3_g_or_v;
	private String jinbinum, personnum, name, contents, quanxian, is_shangxian;
	private String[] mItems;
	private String slide_path;// 音乐路径
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(PostActivity3Activity.this);
				Utils.showMessage(PostActivity3Activity.this, getResources()
						.getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(PostActivity3Activity.this);
				// Utils.showMessage(PostActivity3Activity.this,
				// results.getRetmsg());
				Intent intent = new Intent(PostActivity3Activity.this,
						ActivityReadingActivity.class);
				try {
					intent.putExtra("actId",
							jiexi_and_tiaozhuan(results.getRetmsg()));
					// isOnLine = getIntent().getBooleanExtra("isOnLine",
					// false);
					intent.putExtra("isOnLine", postActivityData.isLine());

					startActivity(intent);
					PostActivity3Activity.this.finish();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					   
				}
				break;
			case 3:
				Utils.ExitPrgress(PostActivity3Activity.this);
				Utils.showMessage(PostActivity3Activity.this, getResources()
						.getString(R.string.l_xa10));
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.post_activity3);
		slide_path = getIntent().getStringExtra("slide_path");
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		postActivityData = (PostActivityData) getIntent().getSerializableExtra(
				"PostActivity2ActivityOffLine");
		post_activity3_name = (EditText) findViewById(R.id.post_activity3_faqizuzhi);
		post_activity3_contents = (EditText) findViewById(R.id.post_activity3_zuzhijieshao);
		post_activity3_jinbinum = (EditText) findViewById(R.id.post_activity3_jinbinum);
		post_activity3_personnum = (EditText) findViewById(R.id.post_activity3_personnum);
		post_activity3_g_or_v = (LinearLayout) findViewById(R.id.post_activity3_g_or_v);
		post_activity3_quanxian_set = (Spinner) findViewById(R.id.post_activity3_quanxian_set);
		post_activity3_is_dongtai = (CheckBox) findViewById(R.id.post_activity3_is_dongtai);
		mItems = getResources().getStringArray(R.array.clubspace_quanxian);
		ArrayAdapter<String> _Adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, mItems);
		post_activity3_quanxian_set.setAdapter(_Adapter);
		if (postActivityData.isLine()) {
			postActivityData.setIs_online("1");
			post_activity3_g_or_v.setVisibility(View.GONE);
			name = null;
			contents = null;
		} else {
			post_activity3_g_or_v.setVisibility(View.VISIBLE);
			postActivityData.setIs_online("0");
		}
		quanxian = "1";
		is_shangxian = "1";
		post_activity3_quanxian_set
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						if (arg2 == 0) {
							quanxian = "1";
						} else if (arg2 == 1) {
							quanxian = "2";
						}
//						   
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
		this.findViewById(R.id.post_activity_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						PostActivity3Activity.this.finish();
					}
				});
		post_activity3_is_dongtai
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						if (arg1) {
							is_shangxian = "" + 1;
//							   ("是否显示到动态:" + is_shangxian);
						} else {
							is_shangxian = "" + 0;
//							   ("是否显示到动态:" + is_shangxian);
						}
					}
				});
		this.findViewById(R.id.create_club_number2).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// Intent intent = new
						// Intent(PostActivity3Activity.this,
						// ActivityReadingActivity.class);
						// startActivity(intent);
						// overridePendingTransition(R.anim.slide_left,
						// R.anim.slide_right);
						fuzhi();
						post();
					}
				});
	}

	private String jiexi_and_tiaozhuan(String d) throws JSONException {
		JSONObject object = new JSONObject(d);
		return object.getString("activityId");
	}

	private void fuzhi() {
		if (!postActivityData.isLine()) {
			name = post_activity3_name.getText().toString().trim();
			contents = post_activity3_contents.getText().toString().trim();
		}
		jinbinum = post_activity3_jinbinum.getText().toString().trim();
		personnum = post_activity3_personnum.getText().toString().trim();
	}

	private void post() {
		Utils.showPrgress(PostActivity3Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> params = new HashMap<String, Object>();
				try {
					params.put("activity", fengzhuang());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					   
				}
				String content = Utils.sendRequest(params, Constants.BadiUrl_1
						+ "activity/post");
//				   ("俱乐部创建活动提交的数据是:" + params.toString());
				if (content != null) {
//					   ("俱乐部创建活动返回的数据是:" + content);
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						//    ("俱乐部创建活动返回的数据是:"
						// + results.getRetmsg());
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private JSONObject fengzhuang() throws JSONException {
		JSONObject object = new JSONObject();
		object.put("cid", postActivityData.getCid());// （线上AND线下）俱乐部ID
		object.put("uid", Utils.getUid(PostActivity3Activity.this));// （线上AND线下）发布活动的用户ID
		object.put("typeid", postActivityData.getActivityType());// （线上AND线下）活动类别
		object.put("activity_name", postActivityData.getName());// 线上AND线下）活动名称
		object.put("start_time", postActivityData.getStartTime());// （线上AND线下）活动开始时间（格式为：2014-09-22
		// 09:30）
		object.put("end_time", postActivityData.getEndTime());// （线上AND线下）活动结束时间（格式为：2014-09-22
		// 09:30）注意，结束时间一定大于开始时间
		object.put("activity_editor", postActivityData.getActivityContent());// （线上AND线下）（格式为：2014-09-22
		// 09:30）活动内容简介
		object.put("activity_image", postActivityData.getUri_pic());// （线上AND线下）活动封面图

		if (slide_path != null) {
			object.put("music", slide_path);
		}
		// 格式：/Public/Uploads/2014-06/140263893872417.jpg
		if (jinbinum != null || !jinbinum.equals("0")) {
			object.put("is_gold", "1");// （线上AND线下）加入该活动是否需要金币 0：否，1：是
			object.put("activity_gold", jinbinum);// （线上AND线下）若需要金币，那么需要多少金币
			// 当不需要金币加入时，则为0
		} else {
			object.put("is_gold", "0");
			object.put("activity_gold", "0");
		}
		if (personnum != null || !personnum.equals("0")) {
			object.put("is_limit", "1");// （线上AND线下）该活动是否限制人数加入 0：否，1：是
			object.put("activity_limit", personnum);// （线上AND线下）若限制人数加入，那么人数上限，当不限制时则为0
		} else {
			object.put("is_limit", "0");
			object.put("activity_limit", "0");
		}

		object.put("activity_status", quanxian);// （线上AND线下）加入该活动的权限 1：所有人，2：会员
		if (postActivityData.isLine()) {
			object.put("is_online", "1");// （线上AND线下）线上OR线下 0：线下，1：线上
		} else {
			object.put("is_online", "0");
		}
		object.put("activity_organization", "" + name);// （线下）发起组织
		object.put("activity_organization_introduce", "" + contents);// （线下）发起组织介绍
		object.put("activity_class", "" + postActivityData.getShihezhuqun());// （线下）适合族群
		object.put("activity_address", "" + postActivityData.getAdress());// （线下）活动地点
		object.put("map_x", "" + postActivityData.getMap_x());// （线下）活动地点纬度
																// 格式：40.345367
																// 0.0000000
		object.put("map_y", "" + postActivityData.getMap_y());// （线下）活动地点经度
																// 格式：130.569536
																// 0.0000000
		object.put("is_show", is_shangxian);// （线上AND线下）是否显示到动态栏 0：否，1：是

		return object;
	}
}
